#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
#include <extractGraph/extractGraph.h>
#include <multipleImage/multipleImage.h>

using namespace cv;
using namespace std;
using namespace xfeatures2d;

int main()
{
    ExtractGraph extractor;

    vector<String> files;       //存放文件路径
    vector<String> templateName;
    vector<Mat> imgVector;
    glob("../../resource/image/base/*.jpg", files);
    for (int i=0; i<files.size(); i++)
    {
        imgVector.push_back(extractor.extractComponet(imread(files[i])));
        //获取各个器件的名称
        templateName.push_back( files[i].substr(26, files[i].size()-26-4) );
    }
    int imgNum = imgVector.size();
    cout << imgNum << endl;
    //输出各个器件的名称
    for (auto s : templateName)
        cout << s << endl;

    Ptr<SIFT> detector = SIFT::create();
    vector<vector<KeyPoint>> imgKeyPointsVector(imgNum);
    vector<Mat> imgDescritporVector(imgNum);

    for (int i=0; i<imgNum; i++)
    {
        detector->detect(imgVector, imgKeyPointsVector);
        detector->compute(imgVector, imgKeyPointsVector, imgDescritporVector);
    }

    FileStorage fKeyPoints("../getData/data/keyPoints.yaml", FileStorage::WRITE);
    FileStorage fDescriptor("../getData/data/descriptor.yaml", FileStorage::WRITE);
    for (int i=0; i<imgNum; i++)
    {
        fKeyPoints << templateName[i] << imgKeyPointsVector[i];
    }
    fKeyPoints.release();
    for (int i=0; i<imgNum; i++)
    {
        fDescriptor << templateName[i] << imgDescritporVector[i];
    }
    fDescriptor.release();


    waitKey(0);
    destroyAllWindows();
    return 0;
}
